<?php

class Comment extends CActiveRecord
{
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'wp_comments';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('comment_content, comment_author', 'required'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'post' => array(self::BELONGS_TO, 'Post', 'comment_post_ID'),
		);
	}
	
	public function getDeleteUrl(){
		return Yii::app()->createUrl('comment/delete', array(
				'id'=>$this->comment_ID,
				'post_id'=>$this->post->ID,
		));
	}


	/**
	 * Approves a comment.
	 */
/*	public function approve()
	{
		$this->comment_approved=1;
		$this->update(array('comment_approved'));
	}
*/
	/**
	 * @param Post the post that this comment belongs to. If null, the method
	 * will query for the post.
	 * @return string the permalink URL for this comment
	 */
	public function getPostUrl($post=null)
	{
		return Yii::app()->createUrl('post/view', array(
				'id'=>$this->post['ID'],
		));
	}


	/**
	 * @return integer the number of comments that are pending approval
	 */
/*	public function getPendingCommentCount()
	{
		return $this->count('comment_approved=0');
	}
*/
	/**
	 * @param integer the maximum number of comments that should be returned
	 * @return array the most recently added comments
	 */
	public function findRecentComments($limit=10)
	{
		return $this->with('post')->findAll(array(
			'condition'=>'t.comment_approved = 1',
			'order'=>'t.comment_date DESC',
			'limit'=>$limit,
		));
	}

	/**
	 * This is invoked before the record is saved.
	 * @return boolean whether the record should be saved.
	 */
/*	protected function beforeSave()
	{
		if(parent::beforeSave())
		{
			if($this->isNewRecord)
				$this->comment_date=date("Y-m-d H:i:s");
			return true;
		}
		else
			return false;
	}
	*/
}